In [1]:
# Load all importance packages
import geopandas
import numpy as np
import pandas as pd
from shapely.geometry import Point

import missingno as msn

import seaborn as sns
import matplotlib.pyplot as plt

% matplotlib inline
UsageError: Line magic function `%` not found.
In [2]:
road = geopandas.read_file("data/toronto_canada_roads.geojson")
neibour = geopandas.read_file("data/Neighbourhoods.geojson")
#admin = geopandas.read_file("data/toronto_canada_admin.geojson")
build = geopandas.read_file("DA/toronto_canada_osm_buildings.shp")
#land = geopandas.read_file("data/toronto_canada_landusages.geojson")
#places = geopandas.read_file("DA/toronto_canada_osm_places.shp")
#country = geopandas.read_file("data/toronto_canada_places.geojson")
#country = geopandas.read_file("data/toronto_canada_places.geojson")
road.head()
Out[2]:
id osm_id type name tunnel bridge oneway ref z_order access service class geometry
0 1.0 3695758.0 motorway Highway 401 Collectors 0 0 1 401 9.0 None None highway LINESTRING (-79.35227 43.76645, -79.34720 43.7...
1 2.0 3695759.0 motorway Highway 401 Express 0 0 1 401 9.0 None None highway LINESTRING (-79.35366 43.76662, -79.34940 43.7...
2 3.0 3696081.0 motorway Highway 401 Express 0 0 1 401 9.0 None None highway LINESTRING (-79.31491 43.76844, -79.31595 43.7...
3 4.0 3696082.0 motorway Highway 401 Collectors 0 0 1 401 9.0 None None highway LINESTRING (-79.25750 43.78096, -79.25944 43.7...
4 5.0 3696083.0 motorway_link None 0 0 1 None 3.0 None None highway LINESTRING (-79.33431 43.76753, -79.33357 43.7...
In [4]:
#type(country)
neibour.head()
Out[4]:
_id AREA_ID AREA_ATTR_ID PARENT_AREA_ID AREA_SHORT_CODE AREA_LONG_CODE AREA_NAME AREA_DESC X Y LONGITUDE LATITUDE OBJECTID Shape__Area Shape__Length geometry
0 3501 25886861 25926662 49885 94 94 Wychwood (94) Wychwood (94) None None -79.425515 43.676919 16491505 3.217960e+06 7515.779658 POLYGON ((-79.43592 43.68015, -79.43492 43.680...
1 3502 25886820 25926663 49885 100 100 Yonge-Eglinton (100) Yonge-Eglinton (100) None None -79.403590 43.704689 16491521 3.160334e+06 7872.021074 POLYGON ((-79.41096 43.70408, -79.40962 43.704...
2 3503 25886834 25926664 49885 97 97 Yonge-St.Clair (97) Yonge-St.Clair (97) None None -79.397871 43.687859 16491537 2.222464e+06 8130.411276 POLYGON ((-79.39119 43.68108, -79.39141 43.680...
3 3504 25886593 25926665 49885 27 27 York University Heights (27) York University Heights (27) None None -79.488883 43.765736 16491553 2.541821e+07 25632.335242 POLYGON ((-79.50529 43.75987, -79.50488 43.759...
4 3505 25886688 25926666 49885 31 31 Yorkdale-Glen Park (31) Yorkdale-Glen Park (31) None None -79.457108 43.714672 16491569 1.156669e+07 13953.408098 POLYGON ((-79.43969 43.70561, -79.44011 43.705...
In [14]:
fig, ax = plt.subplots(figsize=(150,120))
neibour.plot(ax=ax,color='Gray')
#base =road.plot(ax=ax,color ='white');
#places.plot(ax=base);
#build.plot(ax=base);
#land.plot(ax=base);
#ax.autoscale(enable=True) 
Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x212b4b5b130>
In [21]:
florence = pd.read_csv('data/df_cleaned.csv')
florence.head()
Out[21]:
Unnamed: 0 X Y event_unique_id Occurrence Time Occurrence Date Reported Date Reported Time premisetype ucr_code ucr_ext offence MCI Division Hood_ID Neighbourhood
0 0 -79.385193 43.659229 GO-20149004286 10:55 2014-06-20 2014-06-20 13:20 Apartment 2130 210 Theft Over Theft Over D52 76 Bay Street Corridor (76)
1 1 -79.425400 43.777592 GO-20142411379 00:20 2014-07-02 2014-07-02 02:58 Outside 1457 100 Pointing A Firearm Assault D32 36 Newtonbrook West (36)
2 2 -79.425400 43.777592 GO-20142411379 00:20 2014-07-02 2014-07-02 02:58 Outside 1610 100 Robbery With Weapon Robbery D32 36 Newtonbrook West (36)
3 3 -79.210373 43.801727 GO-20142412127 01:30 2014-07-02 2014-07-02 05:40 House 2120 200 B&E Break and Enter D42 132 Malvern (132)
4 4 -79.254334 43.835884 GO-20142417548 20:52 2014-07-02 2014-07-02 20:57 Commercial 1430 100 Assault Assault D42 130 Milliken (130)
In [22]:
florence = florence.drop(['event_unique_id', 'MCI', 'event_unique_id'], axis=1)
florence
Out[22]:
Unnamed: 0 X Y Occurrence Time Occurrence Date Reported Date Reported Time premisetype ucr_code ucr_ext offence Division Hood_ID Neighbourhood
0 0 -79.385193 43.659229 10:55 2014-06-20 2014-06-20 13:20 Apartment 2130 210 Theft Over D52 76 Bay Street Corridor (76)
1 1 -79.425400 43.777592 00:20 2014-07-02 2014-07-02 02:58 Outside 1457 100 Pointing A Firearm D32 36 Newtonbrook West (36)
2 2 -79.425400 43.777592 00:20 2014-07-02 2014-07-02 02:58 Outside 1610 100 Robbery With Weapon D32 36 Newtonbrook West (36)
3 3 -79.210373 43.801727 01:30 2014-07-02 2014-07-02 05:40 House 2120 200 B&E D42 132 Malvern (132)
4 4 -79.254334 43.835884 20:52 2014-07-02 2014-07-02 20:57 Commercial 1430 100 Assault D42 130 Milliken (130)
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
167520 167520 -79.304466 43.718910 20:30 2018-05-16 2018-05-16 21:57 Outside 2135 210 Theft Of Motor Vehicle D54 43 Victoria Village (43)
167521 167521 -79.297562 43.684856 18:00 2018-05-24 2018-05-25 02:58 Outside 2135 210 Theft Of Motor Vehicle D55 62 East End-Danforth (62)
167522 167522 -79.274391 43.685402 19:00 2018-05-24 2018-05-25 08:59 Outside 2135 210 Theft Of Motor Vehicle D41 122 Birchcliffe-Cliffside (122)
167523 167523 -79.595566 43.686760 17:00 2018-05-24 2018-05-25 11:05 Outside 2135 210 Theft Of Motor Vehicle D23 1 West Humber-Clairville (1)
167524 167524 -79.481178 43.775364 09:00 2018-05-22 2018-05-23 14:48 Other 2135 210 Theft Of Motor Vehicle D32 27 York University Heights (27)

167525 rows × 14 columns

In [24]:
florence['coordinates'] = florence[['X', 'Y']].values.tolist()
florence.head()
Out[24]:
Unnamed: 0 X Y Occurrence Time Occurrence Date Reported Date Reported Time premisetype ucr_code ucr_ext offence Division Hood_ID Neighbourhood coordinates
0 0 -79.385193 43.659229 10:55 2014-06-20 2014-06-20 13:20 Apartment 2130 210 Theft Over D52 76 Bay Street Corridor (76) [-79.3851929, 43.65922929999999]
1 1 -79.425400 43.777592 00:20 2014-07-02 2014-07-02 02:58 Outside 1457 100 Pointing A Firearm D32 36 Newtonbrook West (36) [-79.42539980000001, 43.7775917]
2 2 -79.425400 43.777592 00:20 2014-07-02 2014-07-02 02:58 Outside 1610 100 Robbery With Weapon D32 36 Newtonbrook West (36) [-79.42539980000001, 43.7775917]
3 3 -79.210373 43.801727 01:30 2014-07-02 2014-07-02 05:40 House 2120 200 B&E D42 132 Malvern (132) [-79.21037290000001, 43.801727299999996]
4 4 -79.254334 43.835884 20:52 2014-07-02 2014-07-02 20:57 Commercial 1430 100 Assault D42 130 Milliken (130) [-79.2543335, 43.8358841]
In [25]:
florence['coordinates'] = florence['coordinates'].apply(Point)
florence.head()
Out[25]:
Unnamed: 0 X Y Occurrence Time Occurrence Date Reported Date Reported Time premisetype ucr_code ucr_ext offence Division Hood_ID Neighbourhood coordinates
0 0 -79.385193 43.659229 10:55 2014-06-20 2014-06-20 13:20 Apartment 2130 210 Theft Over D52 76 Bay Street Corridor (76) POINT (-79.38519290000001 43.65922929999999)
1 1 -79.425400 43.777592 00:20 2014-07-02 2014-07-02 02:58 Outside 1457 100 Pointing A Firearm D32 36 Newtonbrook West (36) POINT (-79.42539980000001 43.7775917)
2 2 -79.425400 43.777592 00:20 2014-07-02 2014-07-02 02:58 Outside 1610 100 Robbery With Weapon D32 36 Newtonbrook West (36) POINT (-79.42539980000001 43.7775917)
3 3 -79.210373 43.801727 01:30 2014-07-02 2014-07-02 05:40 House 2120 200 B&E D42 132 Malvern (132) POINT (-79.21037290000001 43.8017273)
4 4 -79.254334 43.835884 20:52 2014-07-02 2014-07-02 20:57 Commercial 1430 100 Assault D42 130 Milliken (130) POINT (-79.2543335 43.8358841)
In [26]:
florence = geopandas.GeoDataFrame(florence, geometry='coordinates')
florence.head()
Out[26]:
Unnamed: 0 X Y Occurrence Time Occurrence Date Reported Date Reported Time premisetype ucr_code ucr_ext offence Division Hood_ID Neighbourhood coordinates
0 0 -79.385193 43.659229 10:55 2014-06-20 2014-06-20 13:20 Apartment 2130 210 Theft Over D52 76 Bay Street Corridor (76) POINT (-79.38519 43.65923)
1 1 -79.425400 43.777592 00:20 2014-07-02 2014-07-02 02:58 Outside 1457 100 Pointing A Firearm D32 36 Newtonbrook West (36) POINT (-79.42540 43.77759)
2 2 -79.425400 43.777592 00:20 2014-07-02 2014-07-02 02:58 Outside 1610 100 Robbery With Weapon D32 36 Newtonbrook West (36) POINT (-79.42540 43.77759)
3 3 -79.210373 43.801727 01:30 2014-07-02 2014-07-02 05:40 House 2120 200 B&E D42 132 Malvern (132) POINT (-79.21037 43.80173)
4 4 -79.254334 43.835884 20:52 2014-07-02 2014-07-02 20:57 Commercial 1430 100 Assault D42 130 Milliken (130) POINT (-79.25433 43.83588)
In [27]:
florence.plot(figsize=(20,10));
In [32]:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')
florence.plot(ax=base, color='Black', marker="*", markersize=100);
In [33]:
police = geopandas.read_file("data/Police Facility Locations.geojson")
police.head()
Out[33]:
_id PROVIDER ADDRESS FACILITY_NAME POSTAL_CODE geometry
0 1 Toronto Police Services 2054 Davenport Road 11 Division M6N 1C8 POINT (-79.46083 43.67107)
1 2 Toronto Police Services 200 Trethewey Drive 12 Division M6M 5E6 POINT (-79.48687 43.69457)
2 3 Toronto Police Services 1435 Eglinton Avenue West 13 Division M6C 3Z4 POINT (-79.43668 43.69832)
3 4 Toronto Police Services 350 Dovercourt Road 14 Division M6J 3E3 POINT (-79.42598 43.65130)
4 5 Toronto Police Services 3699 Bloor Street West 22 Division M9A 1A2 POINT (-79.52918 43.64310)
In [34]:
police.plot(figsize=(30,20), color='#3B3C6E')
Out[34]:
<matplotlib.axes._subplots.AxesSubplot at 0x18da006ce80>
In [39]:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')

florence.plot(ax=base, color='Black', marker="*", markersize=100);


police.plot(ax=base, color='RED',markersize=2200)

_ = ax.axis('off')
plt.legend()
ax.set_title("Crime whithin police radius", fontsize=25)
No handles with labels found to put in legend.
Out[39]:
Text(0.5, 1, 'Crime whithin police radius')
In [40]:
policeboun = geopandas.read_file("data/Police Boundaries Data.geojson")
policeboun.head()
Out[40]:
_id AREA_ID AREA_ATTR_ID PARENT_AREA_ID AREA_SHORT_CODE AREA_LONG_CODE AREA_NAME AREA_DESC X Y LONGITUDE LATITUDE OBJECTID Shape__Area Shape__Length geometry
0 476 25832759 25832759 25832760 4 14 14 14 None None -79.417189 43.649990 15976289 2.710943e+07 30015.196591 MULTIPOLYGON (((-79.40506 43.63307, -79.40664 ...
1 477 25832817 25832817 25832797 3 43 43 43 None None -79.199932 43.766467 15976321 1.137953e+08 53902.463203 MULTIPOLYGON (((-79.13414 43.77397, -79.13419 ...
2 478 25832819 25832819 25832820 1 31 31 31 None None -79.519215 43.748497 15976353 8.177649e+07 44427.808912 MULTIPOLYGON (((-79.58064 43.76302, -79.57754 ...
3 479 25832822 25832822 25832820 2 32 32 32 None None -79.432549 43.756900 15976385 1.170648e+08 43667.613471 MULTIPOLYGON (((-79.38145 43.73024, -79.38190 ...
4 480 25832825 25832825 25832820 3 33 33 33 None None -79.352777 43.764411 15976417 1.012368e+08 45137.976238 MULTIPOLYGON (((-79.31979 43.76836, -79.31971 ...
In [ ]: